package es.uji.viselab.experiment;

import java.awt.Button;
import java.awt.Dialog.ModalityType;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import es.uji.viselab.ViSeLabException;
import es.uji.viselab.benchmark.BenchMarkingRecorder;
import es.uji.viselab.benchmark.DataPackage;
import es.uji.viselab.benchmark.database.DBConnection;
import es.uji.viselab.experiment.control.ExperimentControl;

public class ExperimentPassiveListener extends ExperimentInterface implements ActionListener {

	protected Label labelSource = new Label("Source link: ");
	protected JComboBox availableSources;
	protected Button buttonStartServer = new Button("Start server (wait)");
	protected Button buttonStopServer = new Button("Stop server");

	protected JPanel rootPanel;
	private ExperimentControl experiment;

	/*
	 * public ExPassiveExperimentInterface(NetworkTaskLink tl,BenchMarkingSystem
	 * bm) { super(tl, bm); constructPanel();
	 * availableSources.setSelectedIndex(0); // The source will be ViSeLab
	 * buttonStartServer.setEnabled(false); buttonStopServer.setEnabled(false);
	 * }
	 */
	public ExperimentPassiveListener(BenchMarkingRecorder bm) {
		super(bm);
		constructPanel();
	}

	public ExperimentPassiveListener() {
		// TODO Auto-generated constructor stub
	}

	protected void constructPanel() {

		rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));

		String availableSourceList[] = { "ViSeLab", "ViSP (sockets)","Matlab (sockets)" };
		availableSources = new JComboBox(availableSourceList);
		availableSources.setSelectedIndex(1);

		JPanel line1 = new JPanel();
		line1.setLayout(new FlowLayout());
		((FlowLayout) line1.getLayout()).setAlignment(FlowLayout.CENTER);

		line1.add(labelSource);
		line1.add(availableSources);
		line1.add(buttonStartServer);
		line1.add(buttonStopServer);

		JPanel line2 = new JPanel();
		line2.setLayout(new FlowLayout());
		((FlowLayout) line2.getLayout()).setAlignment(FlowLayout.CENTER);

		buttonStartServer.addActionListener(this);

		rootPanel.add(line1);
		rootPanel.add(line2);

	}

	public JPanel getPanel() {
		return rootPanel;
	}

	private void start() throws ViSeLabException {

		BenchMarkingRecorder bm = new BenchMarkingRecorder();
		BenchMarkingInterface bmi = new BenchMarkingInterface(bm);
		showPanel(bmi.getPanel(),ComponentsConfiguration.BENCHMARKING);
		
		// Here are the experiment parameters
		experiment = new Experiment2();
		experiment.setBenchmarkingSystem(bm);
		
		DBConnection database = new DBConnectionMySQL();
		bm.setDatabase(database );
		
	//	int experimentId= database.setNewExperiment();
		
		Source source = null;
		switch (availableSources.getSelectedIndex()) {
		case 0:
			break;
		case 1:
			source = new VispSource();
			break;
		}
		source.addListener(experiment);
		
//		CameraParameters camParams = experiment.getCameraParameters();

// TODO it would be good to see how the robot and the image frame moves		
//		SceneView scene = new SimpleRobotScene(experiment);
//		showPanel(scene.getPanel(), WindowsConfiguration.SCENE);
//		source.addListener(scene); // Source --> Scene
		
	    source.start();
		experiment.Start();
	}

	/*
	 * @Override public void actionPerformed(ActionEvent ae) { if
	 * (ae.getSource().equals(buttonStartServer)) { start(); } if
	 * (ae.getSource().equals(buttonStopServer)) { stop(); } }
	 */

	private void showPanel(JPanel p, ComponentsConfiguration component) {
		Window topWindow = SwingUtilities.getWindowAncestor(rootPanel);
		JDialog dialog = new JDialog(topWindow, component.getTitle(), ModalityType.MODELESS);
		dialog.getContentPane().add(p);
		dialog.pack();
		dialog.setLocationRelativeTo(topWindow);
		dialog.setSize(component.getWidth(), component.getHeight());
		dialog.setVisible(true);
	}

	private void stop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(buttonStartServer))
			try {
				start();
			} catch (ViSeLabException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

	}

	@Override
	public void newDataArrived(DataPackage dn) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getPanelTitle() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int panelWidth() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int panelHeight() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}

	/*
	 * private void start() { // Link of the information (the source with the
	 * Visual Servoing task) switch (availableSources.getSelectedIndex()) { case
	 * 0: break; case 1: taskLink = new VispTaskLink(); break; default:
	 * LogRecorder.log(LogLevel.ERROR, "Option for source not recognised."); }
	 * taskLink.addDataNotificationListener(this); taskLink.start(); }
	 */

}
